//
//  LoginController.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/1/20.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "LoginController.h"
#import "LoginCodeSendController.h"
#import "LoginInfoModel.h"

#import "PerfectInfoController.h"

#import "JPUSHService.h"


@interface LoginController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoLayoutH;

@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *secureButton;
@property (weak, nonatomic) IBOutlet UIButton *resetPwdButton;
@property (weak, nonatomic) IBOutlet UIButton *doctorTypeButton;
@property (weak, nonatomic) IBOutlet UIButton *counselorTypeButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@end

@implementation LoginController

#pragma mark - Overriod
- (void)dealloc {
    [AppNotificationHelpper removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
    
    self.accountTextField.text = [AppGlobal getLoginAccount];
    
    self.secureButton.app_touchExtendInset = UIEdgeInsetsMake(-10, -10, -10, -10);
    if (Iphone4Screen || Ipad) {
        self.logoLayoutH.constant = 0;
    }
    
    NSArray *btns = @[self.secureButton,
                      self.resetPwdButton,
                      self.doctorTypeButton,
                      self.counselorTypeButton,
                      self.loginButton,
                      self.registerButton];
    
    for (UIButton *btn in btns) {
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [AppNotificationHelpper addObserver:self selector:@selector(clearAccountAndPwdNoti) name:@"_clearAccountAndPwd_" object:nil];
}

#pragma mark - Action
- (void)btnDidClick:(UIButton *)sender { 
    if (sender == self.secureButton) {
        sender.selected = !sender.isSelected;
        self.pwdTextField.secureTextEntry = !sender.isSelected;
        
    }else  if (sender == self.resetPwdButton) {
        LoginCodeSendController *codeSend = [[LoginCodeSendController alloc] initWithTypeResetPwd];
        [self.navigationController pushViewController:codeSend animated:YES];
        
    }else if (sender == self.doctorTypeButton) {
        sender.selected = !sender.isSelected;
        self.counselorTypeButton.selected = !sender.isSelected;
        
    }else  if (sender == self.counselorTypeButton) {
        sender.selected = !sender.isSelected;
        self.doctorTypeButton.selected = !sender.isSelected;
        
    }else  if (sender == self.loginButton) {
        NSString *isCanLogin = [self p_valiteIsCanLogin];
        if (isCanLogin.length != 0) {
            [self.view endEditing:YES];
            [KLProgressHUD showPromptHUDWithMessage:isCanLogin];
            return;
        }
        
        [self request_login];
        
    }else  if (sender == self.registerButton) {
        LoginCodeSendController *codeSend = [[LoginCodeSendController alloc] initWithTypeRegister];
        [self.navigationController pushViewController:codeSend animated:YES];
    }
}

#pragma mark - Request
- (void)request_login {
    KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:@"登录中.."];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"passWord"] = [NSString app_md5:self.pwdTextField.text];
    param[@"phone"] = self.accountTextField.text;
    
    
    NSMutableDictionary *headparam = [NSMutableDictionary dictionary];
    headparam[@"deviceToken"] = [AppGlobal getDeviceToken];

    [AppNetwork configCommonHttpHeaders:headparam];

    WEAKSELF
    [AppNetwork postWithUrl:kLoginAccountUrl params:param success:^(id response) {
        progressHUD.hidden = YES;
        NSMutableDictionary *headparam = [NSMutableDictionary dictionary];
        headparam[@"phone"] = weakSelf.accountTextField.text;
        headparam[@"deviceToken"] = [AppGlobal getDeviceToken];

        [AppNetwork configCommonHttpHeaders:headparam];

        [KLProgressHUD showPromptHUDWithMessage:@"登录成功"];
        
        LoginInfoModel *infoModel = [LoginInfoModel mj_objectWithKeyValues:response[@"partyView"]];
        [LoginInfoModel persistLoginInfo:infoModel];
        
        [AppGlobal updateSid:response[@"sid"]];
        [AppGlobal updateLoginStatus:YES];
        [AppGlobal updateLoginAccount:weakSelf.accountTextField.text];
        [AppGlobal updateLoginPwd:weakSelf.pwdTextField.text];
        
        NSString * deviceToekn =[AppGlobal getDeviceToken];
        NSSet * set;
        if (deviceToekn) {
            set = [NSSet setWithObject:deviceToekn];

        }
        [JPUSHService setTags:set completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
    
        } seq:1];
        
        if (weakSelf.loginSuccessBlock) {
            weakSelf.loginSuccessBlock(infoModel);
        }
        
    } fail:^(AppError *error) {
        progressHUD.hidden = YES;
        [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
    }]; 
}

#pragma mark - Private
- (NSString *)p_valiteIsCanLogin {
    if ([NSString app_isBlankString:self.accountTextField.text] ||
        [NSString app_isBlankString:self.pwdTextField.text]) {
        return @"账号和密码不能为空";
    }
    
    if (self.pwdTextField.text.length > 16 || self.pwdTextField.text.length < 6) {
        return @"请输入6-16位密码";
    }
    
    return nil;
}

#pragma mark - Noti
- (void)clearAccountAndPwdNoti {
    [AppGlobal updateLoginAccount:nil];
    [AppGlobal updateLoginPwd:nil];
    
    self.accountTextField.text = nil;
    self.pwdTextField.text = nil;
}

@end
